/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.service.IFitmentApproveSetupService;
import com.ce.pms.system.renovation.vo.FitmentApproveSetupVO;

/**
 * 功能描述：<br>
 * @author 易贤建 创建日期：2009-1-8<br>
 */
public class FitmentApproveSetupServiceImpl extends PmsBaseServiceImpl
		implements IFitmentApproveSetupService {
	/**
	 * 组织架构Service
	 */
	private IOrganizationalStructureService organizationalStructureService = null;
	/**
	 * 参数service
	 */
	private ISysrefcodeService sysrefcodeService;
	/**
	 * 功能描述:获取第一级审批人列表<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	@SuppressWarnings("unchecked")
	public List<FitmentApproveSetupVO> findFirstApp(){
		List<SysrefcodeDO> list = sysrefcodeService.querySysrefCodesByCode(SysrefcodeDO.RM_REN_REQ_FIRST_APP_EMPLOYEE);

		return assembleSysrefCode2VO(list);
	}
	/**
	 * 功能描述:获取第二级审批人<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	@SuppressWarnings("unchecked")
	public FitmentApproveSetupVO findSecondApp(){
		List<SysrefcodeDO> list = sysrefcodeService.querySysrefCodesByCode(SysrefcodeDO.RM_REN_REQ_SECOND_APP_EMPLOYEE);
		if(list == null || list.isEmpty()) return null;  //如果参数表中没有配置  直接返回空
		if(list.size() != 1)
			throw new BusinessException("system:fitment_app_second_set_error",BusinessException.ERROR);
		return assembleSysrefCode2VO(list.get(0));
	}
	/**
	 * 功能描述:更新所有装修审核设置<br>
	 * @param firseList
	 * @param second
	 * @param deposit
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public void update(List<FitmentApproveSetupVO>firseList,FitmentApproveSetupVO second,FitmentApproveSetupVO deposit){
		updateFirst(firseList); //更新第一审核人
		updateSecond(second); //更新第二审核人
		updateDepost(deposit);//更新保证押金审核人
	}
	/**
	 * 功能描述:更新第一审核人<br>
	 * 这里采取逐个更新,记录不多3-5条记录
	 * @param firseList
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void updateFirst(List<FitmentApproveSetupVO> firseList){
		if(firseList == null || firseList.isEmpty()) return;
		for(FitmentApproveSetupVO setupVO : firseList){
			if(setupVO.getIsAdd()){ //添加
				SysrefcodeDO sysrefcodeDO = assembleFitmentApproveSetupVO2SysrefcodeDO(setupVO);
				sysrefcodeDO.setSysInfo(super.createInfo(setupVO.getCreator()));
				sysrefcodeService.addSysrefCodes(sysrefcodeDO);
			}else if(setupVO.getIsDelete()){ //删除
				sysrefcodeService.deleteSysrefCodesById(setupVO.getSysrefKeyword());
			}
		}
	}
	/**
	 * 功能描述:更新第二审核人<br>
	 * @param second
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void updateSecond(FitmentApproveSetupVO second){
		check(second);
		updateSysrefCode(second);
	}
	/**
	 * 功能描述:更新保证押金审核人<br>
	 * @param deposit
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void updateDepost(FitmentApproveSetupVO deposit){
		check(deposit);
		updateSysrefCode(deposit);
	}
	/**
	 * 功能描述:更新系统参数表<br>
	 * @param setupVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private void updateSysrefCode(FitmentApproveSetupVO setupVO){
		SysrefcodeDO sysrefcode = sysrefcodeService.findSysrefcodeById(setupVO.getSysrefKeyword());
		sysrefcode.setValue(String.valueOf(setupVO.getEmployeeId()));
		sysrefcode.setName(setupVO.getEmployeeName());
	}
	private void check(FitmentApproveSetupVO fitmentApproveSetup){
		checkParameterIsNull(fitmentApproveSetup, "system:fitment_app_param_not_null");
		checkParameterIsNull(fitmentApproveSetup.getSysrefKeyword(),"system:fitment_app_param_error");
		checkParameterIsNull(fitmentApproveSetup.getEmployeeId(),"system:fitment_app_select");
	}
	/**
	 * 功能描述:获取保证金审批人<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	@SuppressWarnings("unchecked")
	public FitmentApproveSetupVO findDepositApp(){
		List<SysrefcodeDO> list = sysrefcodeService.querySysrefCodesByCode(SysrefcodeDO.RM_DEPOSIT_APP_EMPLOYEE);
		if(list == null || list.isEmpty()) return null;  //如果参数表中没有配置  直接返回空
		if(list.size() != 1)
			throw new BusinessException("system:fitment_app_deposit_set_error",BusinessException.ERROR);
		return assembleSysrefCode2VO(list.get(0));
	}

	/**
	 * 功能描述:将系统参数设置的装修转换为装修审批设置VO<br>
	 * @param sysref
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private List<FitmentApproveSetupVO> assembleSysrefCode2VO(List<SysrefcodeDO> sysrefs){
		if(sysrefs == null) return null;

		List<FitmentApproveSetupVO> vos = new ArrayList<FitmentApproveSetupVO>();
		for(SysrefcodeDO sysref : sysrefs){
			vos.add(assembleSysrefCode2VO(sysref));
		}
		return vos;
	}
	/**
	 * 功能描述:将系统参数设置的装修转换为装修审批设置VO<br>
	 * @param sysref
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private FitmentApproveSetupVO assembleSysrefCode2VO(SysrefcodeDO sysref){
		if(sysref == null) return null;

		FitmentApproveSetupVO vo = new FitmentApproveSetupVO();
		vo.setSysrefKeyword(sysref.getId()); //系统参数ID
		//设置员工相关内容
		String employeeID = sysref.getValue();
		if(employeeID == null)
			throw new BusinessException("system:fitment_app_first_set_error",BusinessException.FAILURE);
		vo.setEmployeeName(sysref.getName()); //设置员工姓名

		HmEmployeeDomain domain = organizationalStructureService.getHmEmployeeDomainById(Long.valueOf(employeeID));
		vo.setEmployeeId(domain.getEmployeeId()); //设置员工ID
		vo.setJob(domain.getJob()); //设置职位
		return vo;
	}
	/**
	 * 功能描述:将装修设置转换为系统参数表<br>
	 * @param setupVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	private SysrefcodeDO assembleFitmentApproveSetupVO2SysrefcodeDO(FitmentApproveSetupVO setupVO){
		if(setupVO == null) return null;
		SysrefcodeDO sysrefcode = new SysrefcodeDO();
		sysrefcode.setActive('1');
		sysrefcode.setCode(SysrefcodeDO.RM_REN_REQ_FIRST_APP_EMPLOYEE);
		sysrefcode.setName(setupVO.getEmployeeName());
		sysrefcode.setRefNumber(50050);
		sysrefcode.setRemark(" "); //备注不能为空 就给个空字符串
		sysrefcode.setSeq(1); //序号
		sysrefcode.setValue(String.valueOf(setupVO.getEmployeeId()));
		return sysrefcode;
	}
	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}
	public void setOrganizationalStructureService(
			IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}

}
